package cn.txm.product.service.impl;

import cn.txm.basic.service.impl.BaseServiceImpl;
import cn.txm.product.domain.Product;
import cn.txm.product.domain.ProductDetail;
import cn.txm.product.mapper.ProductMapper;
import cn.txm.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {
    @Autowired
    private ProductMapper productMapper;

    @Override
    @Transactional
    public void saveOrUpdate(Product product) {
        //如果商品id为null,就是新增
        if(product.getId()==null){
            //先保存商品 返回主键
            productMapper.save(product);
            //再保存商品详情
            ProductDetail productDetail = product.getProductDetail();
            productDetail.setProduct_id(product.getId());
            productMapper.saveProductDetail(productDetail);
        }else {
            //有ID就是修改
            productMapper.update(product);
            ProductDetail productDetail = product.getProductDetail();
            productMapper.updateProductDetail(productDetail);
        }
    }

    /**
     * 删除商品，也要删除商品详情表
     * @param id
     */
    @Override
    @Transactional
    public void deleteproductAnddetail(Long id) {
        //先删除商品表
        productMapper.deleteproduct(id);
        //再删除商品详情表
        productMapper.deleteproductdetail(id);
    }

    /**
     * 批量上架
     * @param ids
     */
    @Override
    @Transactional
    public void batchshelves(List<Long> ids) {
        productMapper.batchshelves(ids);
    }
    /**
     * 批量下架
     * @param ids
     */
    @Override
    @Transactional
    public void batchnoshelves(List<Long> ids) {
        productMapper.batchnoshelves(ids);
    }
}
